using System;
using System.IO;
using System.Threading;
using System.IO.Ports;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF;

namespace FEZ_Cobra_Console_Application1
{
    class SdCard
    {
        PersistentStorage sdPS = new PersistentStorage("SD");
        
        public void MountSDCard()
        {           
           sdPS.MountFileSystem();
           Debug.Print("Mounted");
        }

        public void unMountSD()
        {
            sdPS.UnmountFileSystem();
            Debug.Print("Unmounted");
        }
        
        public void WhatsOnSD()
        {

            // Displays available files and folders: 
            Debug.Print("Getting files and folders:");
            if (VolumeInfo.GetVolumes()[0].IsFormatted)
            {
                //FEZ_Shields.KeypadLCD.Clear();
                string rootDirectory =
                     VolumeInfo.GetVolumes()[0].RootDirectory;
                string[] files = Directory.GetFiles(rootDirectory);
                string[] folders = Directory.GetDirectories(rootDirectory);

                Debug.Print("Files available on " + rootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);

                Debug.Print("Folders available on " + rootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }
        }

        public void WriteJPEGtoSD(byte[] bytes)
        {
            FileStream FileHandle;

        //    Debug.Print("Check 1");
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

         //   Debug.Print("Check 2");

            if (File.Exists(rootDirectory + @"\test.jpeg"))
            {
                FileHandle = new FileStream(rootDirectory + @"\test.jpeg", FileMode.Append);
                FileHandle.Write(bytes, 0, bytes.Length);


            }
            else
            {
                FileHandle = new FileStream(rootDirectory + @"\test.jpeg", FileMode.Create);
                FileHandle.Write(bytes, 0, bytes.Length);
            }


       //     Debug.Print("Check 3");

            FileHandle.Close();
       //     Debug.Print("Check 4");
            // byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
            // write the data and close the file 

            // FileHandle.Write(data, 0,data.Length);
            //Debug.Print("Check 5");

        }

        public void WriteTesttoSD()
        {
            Debug.Print("Check 1");
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;

            Debug.Print("Check 2");

            if (File.Exists(rootDirectory + @"\test.txt"))
            {
                File.Delete(rootDirectory + @"\test.txt");
            }

            Debug.Print("Check 3");

            FileStream FileHandle = new FileStream(rootDirectory + @"\test.txt", FileMode.Create);
            Debug.Print("Check 3");
            byte[] data = Encoding.UTF8.GetBytes("This string will go in the file!");
            // write the data and close the file 
            // FileHandle.Write(bytes, 0, bytes.Length);
            FileHandle.Write(data, 0, data.Length);
            Debug.Print("Check 4");
            FileHandle.Close();
        }

        public byte [] ReadfromSD()
        {
            byte[] data = new byte[1];
            try
            {
                // Assume one storage device is available, 
                // access it through NETMF 
                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                FileStream FileHandle = new FileStream(rootDirectory + @"\hello.txt", FileMode.Open, FileAccess.Read);
                data = new byte[FileHandle.Length];
                // write the data and close the file 
                int read_count = FileHandle.Read(data, 0, data.Length);
                FileHandle.Close();
                Debug.Print("The size of data we read is: " +
                              read_count.ToString());
                Debug.Print("Data from file:");
                Debug.Print(new string(Encoding.UTF8.GetChars(data), 0, read_count));
            }
            catch(Exception asa)
            {
                Debug.Print("Problem with SD Card " + asa);
            }
            return data;

        }
    }
}
